package action.FrontAction;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import common.Pagination;
import entity.CroAddress;
import entity.CroCustomer;
import entity.SysCountry;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import service.CroAddressService;
import service.SysCitysService;
import service.SysCountryService;
import service.SysProvienceService;

import javax.servlet.ServletContext;
import java.util.List;

/**
 * Created by xuweijie on 17-5-18.
 */
public class AddressAction extends ActionSupport {

    @Autowired
    private CroAddressService croAddressService;
    @Autowired
    private SysCountryService sysCountryService;
    @Autowired
    private SysProvienceService sysProvienceService;
    @Autowired
    private SysCitysService sysCitysService;

    private List<CroAddress> croAddresses;
    private String addrid;
    private String successResult;
    private String consignee;
    private String address;
    private String phone;
    private String postcode;
    private String country;
    private String provience;
    private String city;


    /*
    * 显示地址
    * */
    public String showaddress(){
        ServletContext context= ServletActionContext.getServletContext();
        CroCustomer croCustomer= (CroCustomer) context.getAttribute("customer");
        croAddresses=croAddressService.listAddressByCustomer(croCustomer);
        return SUCCESS;
    }

    /*
    * 删除地址
    * */
    public String deladdr(){
        if(croAddressService.DeleteAddressById(addrid,"1")){
            successResult="0";
        }else {
            successResult="1";
        }
        return SUCCESS;
    }

    /*
    * 更新地址
    * */
    public String updateaddr(){
        CroAddress croAddress=croAddressService.findAddressById(addrid);
        croAddress.setConsignee(consignee);
        croAddress.setAddress(address);
        croAddress.setPhone(phone);
        croAddress.setPostcode(postcode);
        croAddress.setCountry(sysCountryService.FindCountryById(Integer.parseInt(country)));
        croAddress.setProvience(sysProvienceService.FindProvienceById(Integer.parseInt(provience)));
        croAddress.setCity(sysCitysService.FindCityById(Integer.parseInt(city)));

        if(croAddressService.UpdateAddressByEntity(croAddress)){
            successResult="0";
        }else {
            successResult="1";
        }
        return SUCCESS;
    }


    /*
    *添加新地址
    * */
    public String insertaddr(){
        ServletContext context= ServletActionContext.getServletContext();
        CroCustomer croCustomer= (CroCustomer) context.getAttribute("customer");
        CroAddress croAddress=new CroAddress();
        croAddress.setConsignee(consignee);
        croAddress.setAddress(address);
        croAddress.setPhone(phone);
        croAddress.setPostcode(postcode);
        croAddress.setCountry(sysCountryService.FindCountryById(Integer.parseInt(country)));
        croAddress.setProvience(sysProvienceService.FindProvienceById(Integer.parseInt(provience)));
        croAddress.setCity(sysCitysService.FindCityById(Integer.parseInt(city)));
        croAddress.setCroCustomer(croCustomer);
        if(croAddressService.InsertAddressByEntity(croAddress)){
            successResult="0";
        }else {
            successResult="1";
        }
        return SUCCESS;
    }


    public List<CroAddress> getCroAddresses() {
        return croAddresses;
    }

    public void setCroAddresses(List<CroAddress> croAddresses) {
        this.croAddresses = croAddresses;
    }

    public String getAddrid() {
        return addrid;
    }

    public void setAddrid(String addrid) {
        this.addrid = addrid;
    }

    public String getSuccessResult() {
        return successResult;
    }

    public void setSuccessResult(String successResult) {
        this.successResult = successResult;
    }

    public String getConsignee() {
        return consignee;
    }

    public void setConsignee(String consignee) {
        this.consignee = consignee;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPostcode() {
        return postcode;
    }

    public void setPostcode(String postcode) {
        this.postcode = postcode;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getProvience() {
        return provience;
    }

    public void setProvience(String provience) {
        this.provience = provience;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}
